package com.cl.short_video_java.aspect;

import com.cl.short_video_java.annotation.Login;
import com.cl.short_video_java.annotation.LoginLog;
import com.cl.short_video_java.annotation.NoRepeatSubmit;
import com.cl.short_video_java.common.Const;
import com.cl.short_video_java.common.Result;
import com.cl.short_video_java.exception.WebException;
import com.cl.short_video_java.util.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Author 付
 * @Date 2024-11-01 18:13
 * @Version 1.0
 * @description
 * @since 1.0
 */
@Aspect
@Component
@Slf4j
public class LoginLogAspect {
	// 切点，对注解@NoRepeatSubmit进行切面
	@Pointcut("@annotation(com.cl.short_video_java.annotation.LoginLog)")
	public void pointCut() {
	}

	// 后置通知，在方法执行后操作
	@AfterReturning(pointcut = "pointCut()", returning = "result")
	public void logLogin(JoinPoint joinPoint, Object result) {

		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		// 转换为ServletRequestAttributes
		ServletRequestAttributes sra = (ServletRequestAttributes) requestAttributes;
		// 获取HttpServletRequest实例
		HttpServletRequest request = sra.getRequest();
		// 获取方法的参数，假设第一个参数是用户名
		String token = request.getHeader("token");
		// 获取当前时间
		Date loginTime = new Date();
		// 假设从请求中获取IP地址（这里只是示例，实际需要从请求对象获取）
		String ipAddress = "127.0.0.1";
	}

}